package com.senven.books.modules.system.log.util;

import com.senven.books.framework.web.model.LoginUser;
import com.senven.books.utils.UserContextHolder;
import com.senven.books.utils.WebUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author longshengyi
 * @date 2021/8/13 0013
 **/
public class SessionUtil {
    public static final String TOKEN_TYPE = "tokenType";
    public static final String GRANT_TYPE = "grantType";
    public static final String ACCESS_TOKEN = "accessToken";
    public static final String JWT_TOKEN = "jwtToken";
    public static final String USERNAME = "username";
    public static final String NAME = "name";
    public static final String USER_ID = "userId";
    public static final String ORG_ID = "orgId";
    public static final String ORG_NAME = "orgName";
    public static final String ORG_MAP = "orgMap";
    public static final String GRADE = "grade";
    public static final String TENANT_ID = "tenantId";
    public static final String ROOT_ID = "rootId";
    public static final String AUTH = "auth";
    public static final String ERROR = "error";
    public static final String HOME_PAGE = "homePage";
    public static final String MOBILE = "mobile";
    public static final String MOBILE2 = "mobile2";
    public static final String PWD_STATUS = "pwdStatus";

    public SessionUtil() {
    }

    public static LoginUser getUserInfo() {
        LoginUser userInfo = null;

        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null && authentication.getPrincipal() != null) {
                authentication.getPrincipal();
                userInfo = (LoginUser)authentication.getPrincipal();
            }
        } catch (Exception var2) {
            var2.printStackTrace();
            return new LoginUser();
        }

        if (userInfo == null) {
            userInfo = new LoginUser();
        }

        return userInfo;
    }

    public static HttpServletRequest getRequest() {
        return WebUtils.getRequest();
    }

    public static HttpServletResponse getResponse() {
        return WebUtils.getResponse();
    }

    public static String getUsername() {
        String username = "";

        try {
            if (UserContextHolder.getInstance().getUsername() != null) {
                username = UserContextHolder.getInstance().getUsername();
            }

            return username;
        } catch (Exception var2) {
            var2.printStackTrace();
            return username;
        }
    }



    public static String getAccount() {
        return getUsername();
    }



    public static Long getUserId() {
        Long userId = null;

        try {
            if (UserContextHolder.getInstance().getId() != null) {
                userId = Long.valueOf(UserContextHolder.getInstance().getId());
            }
        } catch (Exception var3) {
            var3.printStackTrace();
            return userId;
        }

        return userId;
    }


}
